<?php
class ConfigControl extends Control {

    function base() {
        C("debug",0);
       // $xml = "./template/weihu/read.xml";
       // p(Xml::toArray(file_get_contents($xml)));
       // die;
        $tplDirs = dir::tree("./template"); //模板风格
        $tpls = array(); //模板风格信息
        $id = 0;
        foreach ($tplDirs as $tpl) {
            $xml = $tpl['path'] . '/read.xml';
            // p(xml::toArray(file_get_contents($xml)));die;
            $tpls[$id] = array();

            if (is_file($xml)) {
                //获得模板风格的URL地址，用于显示模板图片
                $tplUrl = __ROOT__.'/template/'.basename($tpl['path']);
                //获得模板风格的XML定义
                $tpls[$id] = xml::toArray(file_get_contents($xml));
                //模板截图
                $tpls[$id]['short']=$tplUrl .'/'.basename($tpls[$id]['short']);
                //模板风格名称
                $tpls[$id]['style']=basename($tpl['path']);
            }
            $id++;
           
        }
        // p($tpls);die;
        $this->assign("defaultjpg",__ROOT__.'/static/default.png');
        //分配置所有模板风格数据
        // p($tpls);die;
        $this->assign('tpl_styles',$tpls);
        $db = M("config");
        //获得除了模板风格配置的配置项
        $base_config = $db->where("name<>'style'")->all();
        // p($base_config);die;
        //获得模板配置
        $tpl_config = $db->where("name='style'")->find();
        $this->assign("base_config", $base_config);
        $this->assign("tpl_config", $tpl_config);
        $this->display("index");
    }
    //修改配置
    function edit(){
        $data = $_POST;
        $db = M('config');
         foreach($data as $name=>$value){
            $sql = "UPDATE ".C('DB_PREFIX')."config SET value='$value'
                    WHERE name='$name'";
            $db->exe($sql);
         }
         $allConfig = $db->field("name,value")->all();
         $dataConfigFIle = array();
         foreach($allConfig  as $conf){
             $dataConfigFIle[$conf['name']]=$conf['value'];
         }
         // p($dataConfigFIle);die;
         $str = "<?php if(!defined('PATH_HD'))exit;\nreturn ".var_export($dataConfigFIle,true).";\n?>";
         if(@file_put_contents("./config/base.php", $str)){
            $this->success("修改配置项成功 ");
         }else{
             $this->error("配置文件不能写，请修改文件权限");
         }
    }
    //验证码设置
    function setcode() {
        
        $this->display("setcode");
    }
    //水印设置
    function water() {
        
        $this->display("water");
    }
    //写入配置文件
    function setConfig() {
        $path = './config/common.inc.php';
        $config = include $path;
        $config = array_merge($config, $_POST);
        if (!filePutArray($config, $path)) {
            $this->error("修改配置文件失败，请修改文件{$path}权限为777");
        } else {
            $this->success("修改成功");
        }
    }

}

?>
